//
//  DefaultKeyboardNumPad.swift
//  Keyboard
//
//  Created by 周张林 on 17/8/12.
//  Copyright © 2017年 周张林. All rights reserved.
//

import UIKit

/// 数字
class DefaultKeyboardNumPad:DefaultKeyboardWordPad
{
    static let numArray = [
        "1","2","3","4","5","6","7","8","9","0","-","/",":",";","(",")","$","&","@","\"","."
        ,",","?","!","'","space"]
    
    static let punctuationArray = [
        "[","]","{","}","#","%","^","*","+","=","_","\\","|","~","<",">","€","£","¥","⦁","."
        ,",","?","!","'","space"
    ]
    
    var abcBtn:UIButton!
    var num_punctuation:UIButton!
    
    override func addControls()->Void
    {
        for i in  0..<DefaultKeyboardNumPad.numArray.count {
            let btn = DefaultKeyboardBtn.newButton(title: DefaultKeyboardNumPad.numArray[i], tag: i, kbDelegate: self)
            btnArray.append(btn)
            addSubview(btn)
        }
        //数字和符号转换键
        num_punctuation = DefaultKeyboardBtn.newButton(title: "#+=", tag: 27, kbDelegate: self,fontSize:18)
        addSubview(num_punctuation)
        
        //delete
        deleteBtn = DefaultKeyboardBtn.newButton(title: "⌫", tag: 28, kbDelegate: self)
        addSubview(deleteBtn)
        
        //ABC
        abcBtn = DefaultKeyboardBtn.newButton(title: "ABC", tag: 29, kbDelegate: self)
        addSubview(abcBtn)
        
        //回车
        returnBtn = DefaultKeyboardBtn.newButton(title: "↵", tag: 30, kbDelegate: self,fontSize:32)
        addSubview(returnBtn)
    }
    
    override func layoutSubviews() -> Void {
        let ROW:CGFloat = 5
        
        var smallBtnW = (self.frame.size.width - 11*margin)/10;
        let btnH = (self.frame.size.height - 5*margin)/ROW;
        
        
        for i in 0..<10 {
            let btn = self.btnArray[i];
            btn.frame = CGRect(x:margin + CGFloat(i)*(smallBtnW + margin), y:margin+btnH, width:smallBtnW, height:btnH)
        }
        
        for i in 10..<20 {
            let btn = self.btnArray[i];
            btn.frame = CGRect(x:margin + CGFloat(i-10)*(smallBtnW + margin), y:2*margin + btnH*2, width:smallBtnW, height:btnH)
        }
        
        self.num_punctuation.frame = CGRect(x:margin, y:3*margin + 3*btnH, width:smallBtnW * 1.5, height:btnH)
        
        self.deleteBtn.frame = CGRect(x:frame.width-smallBtnW*1.5-margin,y:3*margin + 3*btnH, width:smallBtnW * 1.5, height:btnH)
        
        let margin3 = (self.frame.size.width - 9.5*smallBtnW - 6*margin)/ROW;
        smallBtnW *= 1.29
        for i in 20..<26 {
            let btn = self.btnArray[i]
            btn.frame = CGRect(x:4*margin3 + smallBtnW + CGFloat(i-20)*(smallBtnW + margin), y:3*margin + 3*btnH, width:smallBtnW, height:btnH)
        }
        
        let bigBtnW = (self.frame.size.width - 5*margin)/4;
        self.abcBtn.frame = CGRect(x:margin, y:4*margin + btnH*4, width:bigBtnW, height:btnH);
        
        let lastBtn = btnArray.last
        lastBtn?.frame = CGRect(x:2*margin+bigBtnW, y:4*margin + btnH*4, width:bigBtnW * 2 + margin, height:btnH);
        
        self.returnBtn.frame = CGRect(x:4*margin + 3*bigBtnW, y:4*margin + btnH*4, width:bigBtnW, height:btnH);
    }
    
    override func trasitionWord() -> Void {
        for i in  0..<20 {
            let btn = btnArray[i]
            if isTrans {
                btn.setTitle(DefaultKeyboardNumPad.punctuationArray[i], for: .normal)
                num_punctuation.setTitle("123", for: .normal)
            }else{
                btn.setTitle(DefaultKeyboardNumPad.numArray[i], for: .normal)
                num_punctuation.setTitle("#+=", for: .normal)
            }
        }
    }
}


extension DefaultKeyboardNumPad{
    override func didClick(_ sender:DefaultKeyboardBtn)->Void{
        switch sender.tag
        {
        case 26:
            inputController?.textDocumentProxy.insertText(" ")
        case 27:
            isTrans = !isTrans
            trasitionWord()
            break
        case 28:
            inputController?.textDocumentProxy.deleteBackward()
            break
        case 29:
            delegate?.showWordPad()
        case 30:
            inputController?.textDocumentProxy.insertText("\n")
        default:
            inputController?.textDocumentProxy.insertText((sender.titleLabel?.text)!)
        }
    }
}
